package com.hljx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.hljx.config.Result;
import com.hljx.dto.SysDicItItemDTO;
import com.hljx.dto.SysDictPageDTO;
import com.hljx.entity.SysDictItem;
import com.hljx.service.SysDictItemService;
import com.hljx.vo.SysDictItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 字典项 前端控制器
 * </p>
 *
 * @author gx
 * @since 2023-09-18
 */
@RestController
@RequestMapping("/sysDictItem")
@Tag(name = "字典数据项管理")
public class SysDictItemController {

    @Autowired
    SysDictItemService service;

    @Operation(summary = "字典管理-数据项新增或编辑")
    @PostMapping("/saveOrUpdateDictItem")
    public Result<Boolean> saveOrUpdateDictItem(@RequestBody @Valid SysDicItItemDTO sysDicItItemDTO) {
        return Result.success(service.saveOrUpdateDictItem(sysDicItItemDTO));
    }

    @Operation(summary = "字典管理-数据项删除")
    @GetMapping("/deleteDictItem")
    public Result<Boolean> deleteDictItem(@RequestParam("id") Long id) {
        return Result.success(service.deleteDictItem(id));
    }

    @Operation(summary = "字典管理-修改字典项启用状态:0禁止1启用")
    @GetMapping("/updateDictItemStatus")
    public Result<Boolean> updateDictItemStatus(@RequestParam("id") Long id,
                                                      @RequestParam("status") Integer status) {
        return Result.success(service.updateDictItemStatus(id,status));
    }

    @Operation(summary = "字典管理-数据项分页查询")
    @PostMapping("/listSysDictItemByPage")
    public Result<IPage<SysDictItemVO>> listSysDictItemByPage(@RequestBody SysDictPageDTO sysDictPageDTO) {
        return Result.success(service.listSysDictItemByPage(sysDictPageDTO));
    }

    @Operation(summary = "导出和下载模板")
    @PostMapping("/export")
    public void export(@RequestBody SysDictPageDTO dto, HttpServletResponse response) throws ClassNotFoundException {
        service.export(dto,response);
    }

    @Operation(summary = "导入")
    @GetMapping("/importExcel")
    public void importExcel(@RequestParam("dictId") Long dictId,@RequestParam("file") MultipartFile file, HttpServletResponse response) throws ClassNotFoundException, IOException {
        service.importExcel(dictId,file,response);
    }


    @Operation(summary = "工作量类型查询")
    @GetMapping("/listSysDicT")
    public Result<List<SysDictItem>> listSysDicT() {
        List<SysDictItem> list = service.list(Wrappers.lambdaQuery(SysDictItem.class).eq(SysDictItem::getDictId, 4));
        return Result.success(list);
    }

}
